package CreativePattern.SingletonPattern;

//import CreativePattern.SingletonPattern.DoubleLocking.Singleton;
//import CreativePattern.SingletonPattern.StaticInnerClass.Singleton;

//import CreativePattern.SingletonPattern.Enum.Singleton;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
//单列模式
public class TestSingleton {

    // 使用线程安全的 Set 来存储实例的哈希码
    private static final Set<Integer> hashCodes = new CopyOnWriteArraySet<>();
    public static void main(String[] args) {
        // 创建一个线程数组
        Thread[] threads = new Thread[100];

        // 启动多个线程来测试 CreativeMode.SingletonPattern.DoubleLocking.Singleton
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {

                Singleton instance = Singleton.getInstance();
//                Singleton instance = Singleton.INSTANCE; //枚举使用这个
                hashCodes.add(instance.hashCode()); // 添加实例的哈希码到线程安全的集合
            });
            threads[i].start();
        }

        // 等待所有线程完成
        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 打印去重后的哈希码
        System.out.println("-------------------------");
        for (Integer hashCode : hashCodes) {
            System.out.println(hashCode);
        }
    }
}
